package com.ldk.DB;

import java.sql.*;

public class DBOpt
{
  public static String getMD5(String dbPath,String fileName) throws ClassNotFoundException
  {
	  	String md5 = null;
		// load the sqlite-JDBC driver using the current class loader
		Class.forName("org.sqlite.JDBC");

		Connection connection = null;
		try {
			// create a database connection
			
			String thisPath = dbPath+"/.svn/wc.db";  
            String str1=thisPath.substring(0,1).toLowerCase();  //直接将字符串第一个字母小写  
            String str2=thisPath.substring(1,thisPath.length());//截取字符串第二个以后  
            thisPath=str1+str2;  
            String conString = "jdbc:sqlite://"+ thisPath.replace('\\', '/');
            connection = DriverManager.getConnection(conString);
			Statement statement = connection.createStatement();
			statement.setQueryTimeout(30); // set timeout to 30 sec.
			ResultSet rs = statement.executeQuery("select p.md5_checksum "
												+ " from NODES n,PRISTINE p "
												+ " where n.checksum = p.checksum and n.kind = 'file' and "
												+ " n.local_relpath = '"+fileName+"'" );
			while (rs.next()) {
				// read the result set
				md5 = rs.getString("md5_checksum");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		} finally {
			try {
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				System.err.println(e);
			}
		}
		return md5;
	}
}